﻿using UnityEngine;
using System.Collections.Generic;
using ProtoBuf.Message;

namespace Room
{
    public class NetUnionFightGate : NetUnionFightBase
    {
       
        public Dictionary<long, InstUFGate> gates = new Dictionary<long, InstUFGate>();

        public NetUnionFightGate(RoomUnionFight unionFight, RoomCharactorManager mgr, RoomNetUnionFight net)
            :base(unionFight,mgr,net)
        {
        }

        public override void Enter()
        {
            base.Enter();
            gates.Clear();
        }
        

        public override void Leave()
        {
            base.Leave();
            gates.Clear();
        }

        public override RoomCharactor CreateCharactor(long uuid)
        {
            var gate = gates[uuid];
            charactorManager.CreateUnionGate(gate);
            gates[uuid] = gate;
            return GetCharactor(uuid);
        }

        public override void ClearCharactorData(long uuid)
        {
            base.ClearCharactorData(uuid);

            if (gates.ContainsKey(uuid))
                gates.Remove(uuid);
        }

        /// <summary>
        /// 刷门
        /// </summary>
        /// <param name="msg"></param>
        public void GateRefresh(SCUFGate msg)
        {
            if (msg != null)
            {
                foreach (var gate in msg.gate)
                {
                    GateRefresh(gate);
                }
            }
        }

        /// <summary>
        /// 刷门
        /// </summary>
        /// <param name="msg"></param>
        public void GateRefresh(InstUFGate gate)
        {
            var uuid = gate.guid;
            gates[uuid] = gate;

            var charactor = GetCharactor(uuid,false);
            if(charactor==null)
            {
                charactor = CreateCharactor(uuid);
            }


            if (gate.hp <= 0)
            {
                var dict = Dict.Blo.DictUnionFightGateBlo.GetUnionFightGateByID(gate.id + "");

                states[uuid] = (int)UnionGateState.Destroy;

                if (string.IsNullOrEmpty(dict.GateState))
                {
                    states[uuid] = (int)UnionGateState.Normal;
                }
            }
            else
            {
                states[uuid] = (int)UnionGateState.Normal;
            }
            postions[uuid] = Dict.Blo.DictUnionFightGateBlo.GetPosition(gate.id + "");
            //angles[uuid] = Dict.Blo.DictUnionFightGateBlo.GetAngle(gate.id + "");

            if (states[uuid] == (int)UnionGateState.Destroy)
            {
                UpdateState(uuid, states[uuid], null);
            }
            else
            {
                UpdateState(uuid, states[uuid]);
            }
            
        }

        /// <summary>
        /// 城门摧毁
        /// </summary>
        /// <param name="msg"></param>
        public void GateDestroy(SCUFGateCrash msg)
        {
            var uuid = 0L;
            foreach(var gate in gates)
            {
                uuid = gate.Value.guid;
                if (gate.Value.id == msg.gateId)
                    break;
            }
            states[uuid] = (int)UnionGateState.Destroy;
            var charactor = GetCharactor(uuid);
            if (charactor != null)
            {
                var ai = charactor.ai as RCAIGate;
                ai.SwitchState((int)UnionGateState.Destroy);

                //var ui = charactor.ui as RCUIUnionGate;
                //ui.ShowHpBar(false);
            }
            
            //达成城门摧毁的成就
            if (charactor.Camp != charactorManager.GetSelfCharactor().Camp)
            {
                var unionActor = charactorManager.GetSelfCharactor().ai as RCAIUnionSelf;
                unionActor.AchieveHorner(3);
            }
        }


        /// <summary>
        /// 关闭所有的们
        /// </summary>
        //public void CloseAllGates()
        //{
        //    foreach(var kvp in gates)
        //    {
        //        var gate = GetCharactor(kvp.Key);
        //        if(gate!=null)
        //        {
        //            var ai = gate.ai as RCAIGate;
        //            ai.SetEnableObstacle(true);
        //        }
        //    }
        //}

        //public void ResetGates()
        //{
        //    var aloAllGateExist = true;
        //    var saoAllGateExist = true;
        //    foreach (var kvp in gates)
        //    {
        //        var state = states[kvp.Key];
                
        //        var gate = GetCharactor(kvp.Key);
        //        if (gate != null)
        //        {
        //            var ai = gate.ai as RCAIGate;

        //            Debug.Log("uuid=" + kvp.Key + " " + "[DEBUG state] " + " UUID:" +kvp.Key + (UnionGateState)state + " gate camp:" + ai.Camp + " MY camp:" + net.netBattle.camp);

        //            if (state == (int)UnionGateState.Normal)
        //            {
        //                if(net.netBattle.camp == ai.Camp)
        //                {
        //                    ai.SetEnableObstacle(false);
        //                }
        //                else
        //                {
        //                    ai.SetEnableObstacle(true);
        //                }
        //            }
        //            else if (state == (int)UnionGateState.Destroy)
        //            {
        //                ai.SetEnableObstacle(false);

        //                if (kvp.Value.camp == 0)
        //                {
        //                    saoAllGateExist = false;
        //                }
        //                else if (kvp.Value.camp == 1)
        //                {
        //                    aloAllGateExist = false;
        //                }
        //            }
        //        }
                
        //        if(aloAllGateExist)
        //        {
        //            DestroyTransfers(1);
        //        }
        //        if(saoAllGateExist)
        //        {
        //            DestroyTransfers(0);
        //        }
        //    }
        //}

        void DestroyTransfers(int camp)
        {
#if UNITY_EDITOR
            return;
#endif
            var transfers = Dict.Blo.DictUnionFightTransferBlo.GetAll();
            foreach (var transfer in transfers)
            {
                if (transfer.Camp == camp && charactorManager.GetCharactor(transfer.UFTransferId) != null)
                {
                    charactorManager.DestroyCharactor(transfer.UFTransferId);
                }
            }
        }

    }
}